import numpy as np
import random as rd
import drawNet as dn
def NW(n, k, p):#NW小世界
    G = np.empty((n, n))
    for i in range(0, n):
        G[i][i] = -1
        for j in range(i + 1, n):
            G[i][j] = G[j][i] = -1

    for i in range(0, n):
        for j in range(0, int(k / 2)):
            nxt = (i + j + 1) % n 
            G[i][nxt] = G[nxt][i] = 1
    cnt = n * k / 2
    while(cnt > 0):
        cnt -= 1
        r = rd.random()
        if(r >= p): continue
        while(True):
            u = rd.randint(0, n)
            v = rd.randint(0, n)
            if(u != v and G[u][v] == -1 and G[v][u] == -1):
                G[u][v] = G[v][u] = 1
                break
    return G
dn.drawGraph(NW(10, 4, 0))
dn.drawGraph(NW(20, 4, 0.3))